﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace Admin72H.Controls.ProductPriceNews
{
    public partial class UCProductPriceNews : System.Web.UI.UserControl
    {
        int _pageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {            
            //if (!CommonWeb.CheckLogin())
            //    Response.Redirect("~/Login.aspx");

            //if (!IsPostBack)
            //{
            //    rdToDate.SelectedDate = DateTime.Today;
            //    rdFromDate.SelectedDate = DateTime.Today.AddDays(-10);
            //    LoadProduct();
            //    LoadCity();
            //    LoadPriceType();
            //    LoadData();
            //}
        }

        #region Load Data
        void LoadData()
        {
            bindData();
        }

        private void LoadProduct()
        {
            //AMService.AMServiceClient AMServiceClient = CommonWeb.GetCurrent_AMService_Client();

            //var returnObject = AMServiceClient.GetListAllProduct();

            //if (returnObject.RetCode != (int)Common.CommonEnums.RetCode.SUCCESS)
            //    return;

            //ddlProduct.DataSource = returnObject.RetObject;
            //ddlProduct.DataTextField = "ProductName";
            //ddlProduct.DataValueField = "ProductId";
            //ddlProduct.DataBind();
            //ddlProduct.Items.Insert(0,new ListItem("Tất cả",""));
        }

        private void LoadPriceType()
        {
            //AMService.AMServiceClient AMServiceClient = CommonWeb.GetCurrent_AMService_Client();

            //var returnObject = AMServiceClient.GetListAllPriceType();

            //if (returnObject.RetCode != (int)Common.CommonEnums.RetCode.SUCCESS)
            //    return;

            //ddlPriceType.DataSource = returnObject.RetObject;
            //ddlPriceType.DataTextField = "Name";
            //ddlPriceType.DataValueField = "Id";
            //ddlPriceType.DataBind();
            //ddlPriceType.Items.Insert(0, new ListItem("Tất cả", "-1"));
        }

        private void LoadCity()
        {
            //AMService.AMServiceClient AMServiceClient = CommonWeb.GetCurrent_AMService_Client();

            //var returnObject = AMServiceClient.GetListAllCity();

            //if (returnObject.RetCode != (int)Common.CommonEnums.RetCode.SUCCESS)
            //    return;

            //ddlCity.DataSource = returnObject.RetObject;
            //ddlCity.DataTextField = "Name";
            //ddlCity.DataValueField = "Id";
            //ddlCity.DataBind();
            //ddlCity.Items.Insert(0, new ListItem("Tất cả", "-1"));
        }
        #endregion

        #region Grid Event
        protected void bindData()
        {
            //try
            //{
            //    RadGrid.DataSource = new object[0];
            //    RadGrid.VirtualItemCount = 0;
            //    RadGrid.DataBind();
            //    AMService.AMServiceClient AMServiceClient = CommonWeb.GetCurrent_AMService_Client();

            //    var returnObject = AMServiceClient.GetListProductPriceNewsByCondition(ddlProduct.SelectedValue, txtName.Text, int.Parse(ddlCity.SelectedValue), int.Parse(ddlPriceType.SelectedValue), (DateTime)rdFromDate.SelectedDate, (DateTime)rdToDate.SelectedDate, _pageIndex, int.MaxValue);

            //    if (returnObject.RetCode != (int)Common.CommonEnums.RetCode.SUCCESS)
            //        return;

            //    RadGrid.DataSource = returnObject.RetObject;
            //    RadGrid.VirtualItemCount = returnObject.RetObject.Length;
            //    RadGrid.PageSize = PageConst.PAGES_SIZE;
            //    RadGrid.DataBind();
            //}
            //catch (Exception exception)
            //{
            //}
        }

        protected void RadGrid_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
        {
            //_pageIndex = e.NewPageIndex + 1;
        }

        protected void RadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            //try
            //{
            //    if ((e.Item is GridEditableItem && e.Item.IsInEditMode))//for remove duplicate caption and edit form template
            //    {
            //        GridEditableItem item = (GridEditableItem)e.Item;
            //        try
            //        {
            //            ((System.Web.UI.WebControls.TableRow)(item.Controls[1].Controls[0].Controls[1].Controls[0].Controls[0])).Visible = false;
            //        }
            //        catch
            //        {
            //        }
            //    }

            //    if (e.Item is GridDataItem)//for item template
            //    {
            //        if (e.Item is GridDataItem)//for item template
            //        {
            //            Label lblSTT = (Label)e.Item.FindControl("lblSTT");
            //            if (lblSTT != null)
            //                lblSTT.Text = (e.Item.ItemIndex + 1).ToString();
            //        }

            //        Image imgUpdate = (Image)e.Item.FindControl("imgUpdate");
            //        Image imgDelete = (Image)e.Item.FindControl("imgDelete");

            //        string Id = ((ProductPriceNewsFull)e.Item.DataItem).Id.ToString();
            //        string script = string.Format("document.getElementById('{0}').value='{1}'; return true;", hdId.ClientID, Id);
            //        imgUpdate.Attributes.Add("onclick", script);
            //        imgDelete.Attributes.Add("onclick", script);
            //    }
            //}
            //catch (Exception)
            //{
            //}
        }

        protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            //bindData();
        }

        protected void imgAdd_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            //frmProductPriceNews.Height = 450;
            //frmProductPriceNews.Title = "Thêm tài khoản";
            //frmProductPriceNewsWindow.NavigateUrl = "frmProductPriceNews.aspx";
            //frmProductPriceNewsWindow.Visible = true;
            //frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgUpdate_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            //frmProductPriceNews.Height = 450;
            //frmProductPriceNewsWindow.Title = "Chỉnh sửa tài khoản";
            //frmProductPriceNewsWindow.NavigateUrl = "frmProductPriceNews.aspx?ProductPriceNewsId=" + hdId.Value;
            //frmProductPriceNewsWindow.Visible = true;
            //frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }

        protected void imgDelete_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            //RadWindow frmProductPriceNewsWindow = frmProductPriceNews;
            //frmProductPriceNews.Height = 50;
            //frmProductPriceNewsWindow.Title = "Xóa tin";
            //frmProductPriceNewsWindow.NavigateUrl = "frmComfirmDelete.aspx?ProductPriceNewsId=" + hdId.Value;
            //frmProductPriceNewsWindow.Visible = true;
            //frmProductPriceNewsWindow.VisibleOnPageLoad = true;
        }
        #endregion

        #region Click
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //RadGrid.CurrentPageIndex = 0;
            //_pageIndex = 1;
            //bindData();
        }
        #endregion    
    }
}